package com.iptv.grape.ui.frgmt;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.*;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.iptv.grape.R;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.domain.Channel;
import com.iptv.grape.domain.IListViewShowItem;
import com.iptv.grape.ui.BaseFrgmt;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.view.ListViewVScroll;
import com.iptv.grape.ui.control.ad.ListViewAdapter;
import com.iptv.grape.ui.control.ad.ListViewUI;
import com.iptv.grape.ui.control.ad.ListViewUIList;

import java.util.List;

// test_fragment.xml
public class LiveMenuFragment extends BaseFrgmt {
    //private static String ARG_PARAM = "param_key";
    //private String mParam;


    private int selectIndex = -1;
    private boolean needSetSelectIndex = false;

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    public boolean isNeedSetSelectIndex() {
        return needSetSelectIndex;
    }

    public void setNeedSetSelectIndex(boolean needSetSelectIndex) {
        this.needSetSelectIndex = needSetSelectIndex;
    }

    private int topMenuListViewItemHeight = 55;


    private ListViewUIList listViewUIList = new ListViewUIList();
    @Override
    protected int layoutId() {
        return R.layout.live_menu_fragment;
    }

    @Override
    protected void initView(@NonNull View root, @Nullable Bundle savedInstanceState) {
        listViewUIList.add(new ListViewUI((ListViewVScroll)root.findViewById(R.id.topMenuList),
                (ImageView)root.findViewById(R.id.iv_view_focus0), (RelativeLayout)root.findViewById(R.id.relbill0),3,55,new ListViewAdapter(inflater,mActivity)));
        listViewUIList.add(new ListViewUI((ListViewVScroll)root.findViewById(R.id.listScroll1),(ImageView)root.findViewById(R.id.iv_view_focus1),
                (RelativeLayout)root.findViewById(R.id.relbill1), 5,55,new ListViewAdapter(inflater,mActivity)));
        listViewUIList.add(new ListViewUI((ListViewVScroll)root.findViewById(R.id.listScroll2),(ImageView)root.findViewById(R.id.iv_view_focus2),
                (RelativeLayout)root.findViewById(R.id.relbill2), 3,55,new ListViewAdapter(inflater,mActivity)));
        listViewUIList.add(new ListViewUI((ListViewVScroll)root.findViewById(R.id.listScroll3),(ImageView)root.findViewById(R.id.iv_view_focus3),
                (RelativeLayout)root.findViewById(R.id.relbill3), 3,55,new ListViewAdapter(inflater,mActivity)));
        listViewUIList.add(new ListViewUI((ListViewVScroll)root.findViewById(R.id.listScroll4),(ImageView)root.findViewById(R.id.iv_view_focus4),
                (RelativeLayout)root.findViewById(R.id.relbill4), 3,55,new ListViewAdapter(inflater,mActivity)));
        listViewUIList.get(0).requestFocus();

        listViewUIList.start();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setSelection();
            }
        }, 100);
        //this.listViewVScroll.setSelection(style.getSelection());
    }

//    // 定位到this.selectIndex中的焦点上
//    private void setSelection(){
//        AppLog.getCurLog().e("FRGMT>>>"+needSetSelectIndex);
//        if(this.needSetSelectIndex && this.selectIndex >= 0 && this.selectIndex < 5) {
//            listViewUIList.setCurFocusIndex(0);
//            ListViewUI listViewUI = listViewUIList.get(0);
//            listViewUI.getListViewVScroll().setSelection(this.selectIndex);
//            listViewUI.getListViewVScroll().setFocusIndex(this.selectIndex);
//            ListViewAdapter lvAdapter = (ListViewAdapter) listViewUI.getListViewVScroll().getAdapter();
//            IListViewShowItem item = lvAdapter.getListViewShowItem(this.selectIndex);
//
//            List<IListViewShowItem> chList = item.getChList();
//
//            AppLog.getCurLog().e("FRGMT>>>"+chList.get(0).getListViewStyle().getDataType());
//            if (chList != null && chList.size() > 0) {
//                listViewUI.getNextListViewUI().show();
//                listViewUI.getNextListViewUI().refresh(chList);
//            } else {
//                listViewUI.getNextListViewUI().hide();
//            }
//            listViewUIList.reSetFocus();
//        }
//    }



    // 定位到this.selectIndex中的焦点上
    private void setSelection(){
        if(this.needSetSelectIndex && this.selectIndex >= 0 && this.selectIndex < 5) {
            listViewUIList.setCurFocusIndex(0);
            ListViewUI listViewUI = listViewUIList.get(0);

            listViewUI.getListViewVScroll().setSelection(this.selectIndex);
            listViewUI.getListViewVScroll().setFocusIndex(this.selectIndex);
            ListViewAdapter lvAdapter = (ListViewAdapter) listViewUI.getListViewVScroll().getAdapter();
            IListViewShowItem item = lvAdapter.getListViewShowItem(this.selectIndex);

            List<IListViewShowItem> chList = item.getChList();
            if (chList != null && chList.size() > 0) {
                listViewUI.getNextListViewUI().show();
                listViewUI.getNextListViewUI().refresh(chList);
            } else {
                listViewUI.getNextListViewUI().hide();
            }
            listViewUIList.reSetFocus();
        }else{
            int index = listViewUIList.getCurFocusIndex();
            ListViewUI listViewUI=  listViewUIList.get(index);
            ListViewAdapter lvAdapter = (ListViewAdapter) listViewUI.getListViewVScroll().getAdapter();
            List<IListViewShowItem> mlist = lvAdapter.getDataList();
          String type=mlist.get(0).getListViewStyle().getDataType();
          if(!"Channel".equalsIgnoreCase(type))
              return;

            int slctItem = 0;
            for(int i = 0;i<mlist.size(); i++){
                Channel channel = (Channel)mlist.get(i);
                String channelID= GrapeContext.curGrapeContext().getChannelDLinkList().getChannelHis().getCurPlayChannel().getChannelID();
                if(channel.getKeyID().equalsIgnoreCase(channelID)){
                    slctItem = i;
                    break;
                }
            }
            listViewUI.getListViewVScroll().setSelection(slctItem);
            listViewUI.getListViewVScroll().setFocusIndex(slctItem);

            List<IListViewShowItem> chilList = lvAdapter.getListViewShowItem(slctItem).getChList();
            if (null==chilList||chilList.size()<1) {
                listViewUI.getNextListViewUI().hide();
            } else {
                listViewUI.getNextListViewUI().show();
                listViewUI.getNextListViewUI().refresh(chilList);
            }
        }
    }

    @Override
    public void onStart(){
        super.onStart();
    }

    @Override
    public  void  onResume(){
        super.onResume();
    }

    public static LiveMenuFragment newInstance() {
        LiveMenuFragment frag = new LiveMenuFragment();
        Bundle bundle = new Bundle();
        //bundle.putString(ARG_PARAM, str);
        frag.setArguments(bundle);   //设置参数
        return frag;
    }

    @Override
    public void onHiddenChanged(boolean hidden){
        super.onHiddenChanged(hidden);
        // 不在最前端界面显示
        if (hidden) {
            // 隐藏
        } else {
            //listViewUIList.get(0).getListViewVScroll().setInitFocus(listViewUIList.get(0).getLastFocusIndex());
            //listViewUIList.get(0).getListViewVScroll().setSelection(listViewUIList.get(0).getLastFocusIndex());
            //listViewUIList.get(0).refresh(GrapeContext.curGrapeContext().getChannelTypeListViewShowItem("live"));

            //listViewUIList.get(0).requestFocus();
            //listViewUIList.get(0).getListViewVScroll().setSelection(2);
            listViewUIList.reSetFocus();

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    setSelection();
                }
            }, 100);
        }
    }


}
